import entity.User;

import java.util.HashMap;
import java.util.Map;

/**
 * @Version :HashMapXXX
 * @PROJECT_NAME: java-basics
 * @PACKAGE_NAME:PACKAGE_NAME
 * @NAME: HashMapExample
 * @author:xuandian
 * @DATE: 2022/6/25 11:18
 * @description:
 */


public class HashMapExample {
    private Map<Integer, User> map = new HashMap<Integer, User>();

    /**
     * 添加
     * @param key
     * @param value
     * @return
     */
    User put(Integer key, User value) {
        return map.put(key, value);
    }
    /**
     * 删除
     * @param key
     * @return
     */
    User remove(Integer key) {
        return map.remove(key);
    }


    /**
     * 判断
     * @param key
     * @return
     */
    boolean containsKey(Integer key) {
        return map.containsKey(key);
    }


    void visitByEntrySet() {
        for (Map.Entry<Integer, User> entry : map.entrySet()) {
            System.out.println(entry);
        }
    }


    void visitByKeySet() {
        for (Integer key : map.keySet()) {
            System.out.println("键:" + key + "值:" + map.get(key));
        }
    }


    void visitByValues() {
        for (User user : map.values()) {
            System.out.println("值:" + user);
        }
    }

    public static void main(String[] args) {
        HashMapExample hashMapExample = new HashMapExample();
        //添加数据
        hashMapExample.put(1, new User(1, "小王"));
        hashMapExample.put(2, new User(2, "小于"));
        hashMapExample.put(3, new User(3, "小刘"));
        hashMapExample.put(4, new User(4, "小马"));
        //移除数据
        User user = hashMapExample.remove(4);
        if (user != null) {
            System.out.println("移除" + user + "成功");
        }

        //是否存在key
        if ( hashMapExample.containsKey(1)) {
            System.out.println("存在key1");
        }

        //EntrySet遍历方式
        hashMapExample.visitByEntrySet();
        System.out.println("----------------");
        //keyset遍历方式
        hashMapExample.visitByKeySet();
        System.out.println("----------------");
        //vaules()遍历方式
        hashMapExample.visitByValues();
    }

}
